#include "StdAfx.h"
#include "PhysicsEntityBody.h"


PhysicsEntityBody::PhysicsEntityBody(UltimatePhysicsActorDesc *actorDesc,UltimatePhysicsScene* scene,Ogre::SceneNode *node, bool isKinematic, bool isStatic, int collisionGroup, int weight)
{
	physXScene = scene;
	parentNode = node;
	Ogre::Vector3 position = parentNode->_getDerivedPosition();
	Ogre::Quaternion orientation = parentNode->_getDerivedOrientation();

	actor = physXScene->createPhysicsActor(actorDesc, position, orientation, isKinematic, isStatic, collisionGroup,weight);

	//if (isStatic) 
	//	actorDesc->body = 0;
	//else
	//	actorDesc->body = new NxBodyDesc();
	//actorDesc->density = 10;
	//actorDesc->globalPose.t 	= NxVec3(position.x,
	//			position.y,
	//			position.z);	
	//	
	//bool valid = actorDesc->isValid();
	//actor = physXScene->createActor(*actorDesc);


	//actor->setGlobalOrientationQuat(NxQuat(NxVec3(orientation.x,orientation.y,orientation.z),orientation.w));
	//if (isKinematic) actor->raiseBodyFlag(NX_BF_KINEMATIC);	//KINEMATIKUS ACTOR

	//// Setting CollisionGroup
	//actor->setGroup(collisionGroup);
	////Get all shapes of the actor
	//NxShape*const* shapes = actor->getShapes();
	//int nShapes = actor->getNbShapes();
	//for (int i = 0; i <  nShapes; i++)
	//{
	//	// Set the given group for all shapes
	//	shapes[i]->setGroup(collisionGroup);
	//}

}


PhysicsEntityBody::~PhysicsEntityBody(void)
{

	physXScene->destroyPhysicsActor(actor);

	delete entityName;
}


void PhysicsEntityBody::setPosition(Ogre::Vector3 position)
{

	lastPosition = position;
	actor->setGlobalPosition(position);


}

Ogre::Vector3 PhysicsEntityBody::getPosition()
{
	return Ogre::Vector3(actor->getGlobalPosition().x, actor->getGlobalPosition().y, actor->getGlobalPosition().z);
}

void PhysicsEntityBody::setOrientation(Ogre::Quaternion orientation)
{
	lastOrientation = orientation;
	actor->setGlobalOrientation(orientation);

}

Ogre::Quaternion PhysicsEntityBody::getOrientation()
{
	return actor->getGlobalOrientation();
}


void PhysicsEntityBody::setLocalPosition(Ogre::Vector3 position)
{
	
}
Ogre::Vector3 PhysicsEntityBody::getLocalPosition()
{
	return actor->getGlobalPosition();

}

void PhysicsEntityBody::setLocalOrientation(Ogre::Quaternion orientation)
{

}

Ogre::Quaternion PhysicsEntityBody::getLocalOrientation()
{
	return actor->getGlobalOrientation();
}

void PhysicsEntityBody::setLookAt(Ogre::Vector3 target)
{
	actor->setLookAt(target);


}
	
Ogre::Vector3 PhysicsEntityBody::getFacingDir()
{
	

	return actor->getFacingDir();
}


void PhysicsEntityBody::addForce(Ogre::Vector3 force)
{
	/*actor->setLinearVelocity(NxVec3(0,0,0));
	actor->setAngularVelocity(NxVec3(0,0,0));*/
	actor->addForce(force);
}

void PhysicsEntityBody::addTorque(Ogre::Vector3 torque)
{
	/*actor->setLinearVelocity(NxVec3(0,0,0));
	actor->setAngularVelocity(NxVec3(0,0,0));*/
	actor->addTorque(torque);
}


void PhysicsEntityBody::setEntityNameForUserData(Ogre::String *name)
{
	entityName = name;
	actor->setUserData(name);
}

void PhysicsEntityBody::disableGravity()
{
	actor->disableGravity();

}

Ogre::SceneNode*  PhysicsEntityBody::getParentNode()
{
	return parentNode;
}

void PhysicsEntityBody::killBody()
{
	actor->setKinematic(false);
}